package com.bianguo.android.functiondemo.view.activity;

import android.annotation.SuppressLint;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.internal.BottomNavigationItemView;
import android.support.design.internal.BottomNavigationMenuView;
import android.support.design.widget.BottomNavigationView;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.bianguo.android.functiondemo.R;
import com.bianguo.android.functiondemo.adapter.ListViewAdapter;
import com.bianguo.android.functiondemo.utils.BottomNavigationViewHelper;
import com.bianguo.android.functiondemo.view.widget.RickyListView;

import java.lang.reflect.Field;
import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

public class ListViewActivity extends BaseActivity implements AdapterView.OnItemClickListener, BottomNavigationView.OnNavigationItemSelectedListener {

    @BindView(R.id.list_view)
    RickyListView mListView;
    @BindView(R.id.list_bottom_navigation_view)
    BottomNavigationView bottomNavigationView;

    private ArrayList<String> list;
    private ListViewAdapter adapter;

    @Override
    public int layoutId() {
        return R.layout.activity_list_view;
    }

    @Override
    public void initView(@Nullable Bundle savedInstanceState) {
        list = new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            list.add("item --> " + i);
        }
        adapter = new ListViewAdapter(mContext, list);
        mListView.setAdapter(adapter);
        mListView.setOnItemClickListener(this);
//        disableShiftMode(bottomNavigationView);
//        BottomNavigationViewHelper.disableShiftMode(bottomNavigationView);
        bottomNavigationView.setOnNavigationItemSelectedListener(this);
    }

    @SuppressLint("RestrictedApi")
    private void disableShiftMode(BottomNavigationView bottomNavigationView) {
        BottomNavigationMenuView menuView = (BottomNavigationMenuView) bottomNavigationView.getChildAt(0);
        try {
            Field mShiftingMode = menuView.getClass().getDeclaredField("labelVisibilityMode");
            mShiftingMode.setAccessible(true);
            mShiftingMode.setInt(menuView, 1);
            mShiftingMode.setAccessible(false);
            int childCount = menuView.getChildCount();
            for (int i = 0; i < childCount; i++) {
                BottomNavigationItemView itemView = (BottomNavigationItemView) menuView.getChildAt(i);
                itemView.setLabelVisibilityMode(1);
                itemView.setChecked(itemView.getItemData().isChecked());
            }
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {

    }

    @OnClick({R.id.list_release_btn})
    public void onClickBk(View view) {
        switch (view.getId()) {
            case R.id.list_release_btn:
                showToast("发布");
                break;
        }
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
        switch (menuItem.getItemId()) {
            case R.id.menu_bottom_home:
//                showToast("home");
                break;
            case R.id.menu_bottom_service:
//                showToast("service");
                break;
            case R.id.menu_bottom_news:
//                showToast("news");
                break;
            case R.id.menu_bottom_me:
//                showToast("me");
                break;
        }
        return true;
    }
}
